package com.simulation.web.controller.tech;

import cn.hutool.core.bean.BeanUtil;
import com.simulation.common.core.domain.AjaxResult;
import com.simulation.web.dto.TechCommentDTO;
import com.simulation.web.domain.TechComment;
import com.simulation.web.service.impl.CommentServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("项目-评论")
@RestController
@RequestMapping("techComment")
@AllArgsConstructor
public class TechCommentController {

    private final CommentServiceImpl commentService;

    @GetMapping("getPage")
    @ApiOperation("分页查询评论")
    public AjaxResult getPage(TechCommentDTO techCommentDTO) {
        return AjaxResult.success(commentService.getPage(techCommentDTO));
    }

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @RequestMapping("list")
    public List<TechComment> listAll() {
        return commentService.listAll();
    }


    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回记录，没有返回null
     */
    @RequestMapping("getById")
    public TechComment getById(Long id) {
        return commentService.getById(id);
    }    
     
    /**
     * 新增，忽略null字段
     *
     * @param techCommentDTO 新增的记录
     * @return 返回影响行数
     */
    @PostMapping("insert")
    @ApiOperation("新增查询")
    public AjaxResult insert(@RequestBody TechCommentDTO techCommentDTO) {
        TechComment techComment = new TechComment();
        BeanUtil.copyProperties(techCommentDTO, techComment);
        commentService.insertIgnoreNull(techComment);
        return AjaxResult.success();
    }    
      
    /**
     * 修改，忽略null字段
     *
     * @param techComment 修改的记录
     * @return 返回影响行数
     */
    @RequestMapping("update")
    public int update(@RequestBody TechComment techComment) {
        return commentService.updateIgnoreNull(techComment);
    }
    
    /**
     * 删除记录
     *
     * @param techCommentDTO 待删除的记录
     * @return 返回影响行数
     */
    @PostMapping("delete")
    @ApiOperation("删除评论")
    public AjaxResult delete(@RequestBody TechCommentDTO techCommentDTO) {
        commentService.delete(techCommentDTO.getIdList());
        return AjaxResult.success();
    }
    
}